﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhaoxi.MicroService.Interface;
using Zhaoxi.MicroService.Model;

namespace Zhaoxi.MSAProject.NET6Preview3Open.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly ILogger<TestController> _logger;
        private readonly IUserService _iUserService = null;

        public TestController(ILogger<TestController> logger, IUserService userService)
        {
            _logger = logger;
            this._iUserService = userService;
        }

        [HttpGet]
        [Route("Index")]
        public IActionResult Index()
        {
            var users = this._iUserService.UserAll().Select(u => new User()
            {
                Id = u.Id,
                LoginTime = u.LoginTime,
                Account = $"{u.Account}+ V{DateTime.Now.Day}",
                Email = u.Email,
                Name = $"{u.Name}+ V{DateTime.Now.Day}",
                Password = u.Password,
                Role = u.Role
            });//三层架构

            return new JsonResult(users);
        }


        private static List<string> StringList = new List<string>();
        private static long TotalCount = 0;

        /// <summary>
        /// 1就持续增加   0就清除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GC")]
        public IActionResult TestGCKeep(int id)
        {
            TotalCount += 1;

            Console.WriteLine($"This is {TotalCount} Invoke TestGCKeep1 id={id}");

            #region MyRegion
            if (id == 0)
            {
                StringList.Clear();
                return new JsonResult(new
                {
                    Result = true,
                    Value = new
                    {
                        Heap = GC.GetTotalMemory(false) / 1024 / 1024,
                        TotalCount = TotalCount
                    },
                    Message = "Clear All MemoryUse"
                });
            }
            #endregion
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 3800 * 20; i++)
            {
                stringBuilder.Append("我深深的理解，耗费了多少时间，战胜了多少困难，你才取得眼前的成绩。而未来的征程里，将还有更多的艰难险阻等着你去战胜，去征服!请你相信，在你追求、拼搏和苦干的过程中，朝夕教育将永远站在你的身旁！");
            }
            string str = stringBuilder.ToString();
            StringList.Add(str);

            return new JsonResult(new
            {
                Result = true,
                Value = new
                {
                    Heap = GC.GetTotalMemory(false) / 1024 / 1024,
                    TotalCount = TotalCount
                },
                Message = $"第{TotalCount}次占据内存"
            });
        }

    }
}
